package com.wxzl.community.common.widget;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wxzl.community.common.R;
import com.wxzl.community.common.utils.ext.AnyKt;

import java.util.ArrayList;
import java.util.List;


public class ZoomImageActivity extends AppCompatActivity {
    private ArrayList<String> urls;
    private int               position = 0;
    private TextView          tip;
    private String            tipText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager_img);
        urls = getIntent().getStringArrayListExtra("img");
        position = getIntent().getIntExtra("position", 0);
        ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
        tip = (TextView) findViewById(R.id.viewpager_tip);
        tipText = getResources().getString(R.string.viewpager_tip);
        tip.setText(String.format(tipText, position + 1, urls.size()));
        if (urls == null || urls.size() < 0) {
            Toast.makeText(ZoomImageActivity.this, "图片错误", Toast.LENGTH_LONG).show();
        } else {
            mViewPager.setAdapter(new TouchImageAdapter(this, urls));
            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageSelected(int arg0) {
                    tip.setText(String.format(tipText, arg0 + 1, urls.size()));
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                }
            });
            mViewPager.setCurrentItem(position);
        }

    }

    static class TouchImageAdapter extends PagerAdapter {
        private final Activity     activity;
        private final List<String> urls;

        public TouchImageAdapter(Activity activity, List<String> urls) {
            this.activity = activity;
            this.urls = urls;
        }

        @Override
        public int getCount() {
            return urls.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            TouchImageView img = new TouchImageView(container.getContext());
            AnyKt.log(urls.get(position));

//			img.setImageResource(R.mipmap.logo_icon);

            Glide.with(activity).load(urls.get(position))

                    //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                    .skipMemoryCache(false)
                    //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    //设置图片加载的优先级
                    .priority(Priority.HIGH)
                    .placeholder(R.mipmap.camera_add)
                    .error(R.drawable.picture_icon_data_error)

                    .into(img);


            container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            img.setOnSingalTapListener(new TouchImageView.OnSingleTapListener() {

                @Override
                public void onclick() {
                    activity.finish();
                }
            });
            return img;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }
}